<?php

namespace app\api\controller;

use app\api\controller\Base;

/**
 * 订单接口
 */
class Order extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 挂买卖单
     */
    public function index()
    {
    	//1买单 我要买 2卖单 我要卖
    	$type = input('type');
    	if(empty($type)){
    		$this->error('参数错误');
    	}
    	//数量
    	$gold_num = input('gold_num');
    	if($gold_num<=0){
    		$this->error('金币数量错误');
    	}
    	//价格
    	$price = input('price');
    	if($price<=0){
    		$this->error('价格错误');
    	}
    	//判断是否有支付密码
    	if(empty($this->user['paypass'])){
    		$this->error('请设置支付密码');
    	}
    	//支付密码
    	$paypass = input('paypass');
    	if(empty($paypass)){
    		$this->error('请输入支付密码');
    	}
    	//判断支付密码是否正确
    	if($this->user['paypass'] != md5($paypass)){
    		$this->error('支付密码不正确');
    	}
    	//买
    	if($type==1){
    		$data['type'] = $type;
    	}
    	//卖
    	if($type==2){
    		//检测币是否足够
    		$user = db('user')->where(['id'=>$this->user['id']])->find();
    		if($user['gold']<$gold_num){
    			$this->error('金币不足');
    		}
    		//冻结金币
    		$fre['gold'] = $user['gold']-$gold_num;
    		$fre['gold_freeze'] = $user['gold_freeze']+$gold_num;
    		db('user')->where(['id'=>$user['id']])->update($fre);
    		// $data['sell_id'] = $this->user['id'];
    	}
    	$data['put_num'] = date('YmdHis').rand(10000,99999);
        $data['price'] = $price;
    	$data['user_id'] = $this->user['id'];
    	$data['gold_num'] = $gold_num;
    	$data['addtime'] = time();
    	$data['status'] = 1;
    	$data['type'] = $type;
    	// $data['user_id'] = $this->user['id'];
    	$r = db('put_order')->insert($data);
        if($r){
        	$this->success('挂单成功');
        }else{
        	$this->error('挂单失败');
        }
    }
    /**
     * 接单
     */
    public function getOrder(){
    	//1接买单  2接卖单 
    	$type = input('type');
    	if(empty($type)){
    		$this->error('参数错误');
    	}
    	//判断是否有支付密码
    	if(empty($this->user['paypass'])){
    		$this->error('请设置支付密码');
    	}
    	//支付密码
    	$paypass = input('paypass');
    	if(empty($paypass)){
    		$this->error('请输入支付密码');
    	}
    	//判断支付密码是否正确
    	if($this->user['paypass'] != md5($paypass)){
    		$this->error('支付密码不正确');
    	}
    	//挂单号
    	$put_num = input('order_num');
    	if(empty($put_num)){
    		$this->error('挂单号不能空');
    	}
    	$order = db('put_order')->where(['put_num'=>$put_num])->find();
    	//判断订单是否完成
    	if($order['status']!=1){
    		$this->error('该订单已完成,请刷新订单');
    	}
        //数量
        $gold_num = input('gold_num');
        if(empty($gold_num)){
            $this->error('数量不正确');
        }
        if($gold_num<0){
            $this->error('数量不能小于0');
        }
        if($gold_num>($order['gold_num']-$order['out_num'])){
            $this->error('数量不能大于订单数量');
        }
        if($gold_num==($order['gold_num']-$order['out_num'])){
            $data1['status']=2;
        }
    	//接买单
    	if($type==1){
    		//判断自己的金币是否足够
    		if($this->user['gold']<$gold_num){
    			$this->error('可用金币不足');
    		}
    		$data['sell_id'] = $this->user['id'];
            $data['buy_id'] = $order['user_id'];

    	}
    	//接卖单
    	if($type==2){
    		$data['buy_id'] = $this->user['id'];
            $data['sell_id'] = $order['user_id'];
    	}
    	$data['order_num'] = date('YmdHis').rand(10000,99999);
        $data['price'] = $order['price'];
        $data['put_id'] = $order['id'];
        $data['gold_num'] = $gold_num;
        $data['addtime'] = time();
        $data['matchtime'] = time();
        $data['status'] = 2;
        $data['type'] = $type;
    	$r = db('order')->insert($data);
        $data1['out_num'] = $order['out_num']+$gold_num;
        db('put_order')->where(['put_num'=>$put_num])->update($data1);
    	if($r){
    		$this->success('匹配成功');
    	}else{
    		$this->error('匹配失败');
    	}
    }
    /**
     * 点击确认付款
     */
    public function paymoney(){
    	//支付密码
    	$paypass = input('paypass');
    	if(empty($paypass)){
    		$this->error('请输入支付密码');
    	}
    	//判断支付密码是否正确
    	if($this->user['paypass'] != md5($paypass)){
    		$this->error('支付密码不正确');
    	}
    	//订单号
    	$order_num = input('order_num');
    	if(empty($order_num)){
    		$this->error('订单号不能空');
    	}
    	$order = db('Order')->where(['order_num'=>$order_num])->find();
    	//判断是否是自己的订单
    	if($order['buy_id']!=$this->user['id']){
    		$this->error('订单信息错误');
    	}
    	//更改订单状态
    	$data['status'] = 3;
    	$data['paytime'] = time();
    	$r = db('order')->where(['order_num'=>$order_num])->update($data);
    	if($r){
    		$this->success('付款成功');
    	}else{
    		$this->error('付款失败');
    	}
    }
    /**
     * 点击确认付币
     */
    public function paycoin(){
    	//支付密码
    	$paypass = input('paypass');
    	if(empty($paypass)){
    		$this->error('请输入支付密码');
    	}
    	//判断支付密码是否正确
    	if($this->user['paypass'] != md5($paypass)){
    		$this->error('支付密码不正确');
    	}
    	//订单号
    	$order_num = input('order_num');
    	if(empty($order_num)){
    		$this->error('订单号不能空');
    	}
    	$order = db('Order')->where(['order_num'=>$order_num])->find();
    	//判断是否是自己的订单
    	if($order['sell_id']!=$this->user['id']){
    		$this->error('订单信息错误');
    	}
    	//判断卖家是否点击付款
    	if($order['status']!=3){
    		$this->error('等待买家付款');
    	}
    	if($order['type']==1){
    		//减少卖家可用币  买单
    		db('user')->where(['id'=>$this->user['id']])->setDec('gold',$order['gold_num']);
    	}else{
    		//减少卖家冻结的币  卖单
    		db('user')->where(['id'=>$this->user['id']])->setDec('gold_freeze',$order['gold_num']);
    	}
    	//增加买家的币
    	db('user')->where(['id'=>$order['buy_id']])->setInc('gold',$order['gold_num']);
    	//加入财务日志
    	$this->addfinancial($this->user['id'],1,'交易卖出',4,$order['gold_num']);
    	$this->addfinancial($order['buy_id'],2,'交易买入',3,$order['gold_num']);
    	//更改订单状态
    	$data['status'] = 4;
    	$data['cointime'] = time();
    	$r = db('order')->where(['order_num'=>$order_num])->update($data);
    	if($r){
    		$this->success('付币成功');
    	}else{
    		$this->error('付币失败');
    	}
    }
    /**
     * 交易列表
     */
    public function orderList(){
    	//1 买入 2卖出
    	$type = input('type');
    	if($type){
    		$where['type'] = $type;
    	}
    	//页
    	$page = input('page')?input('page'):1;
    	//一页显示数量
    	$limit = input('limit')?input('limit'):10;
    	$where['status'] = 1;
    	$count = db('put_order')->where($where)->count();
    	$list = db('put_order')->field('id,put_num,user_id,gold_num,out_num,price,addtime,type')->where($where)->order('addtime desc')->page($page,$limit)->select();
    	foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['order_num'] = $v['put_num'];
    		$list[$k]['gold_num'] = $v['gold_num']-$v['out_num'];
    		if($v['type']==1){
    			//买单
    			$list[$k]['username'] = db('user')->where(['id'=>$v['user_id']])->find()['username'];
    		}
    		if($v['type']==2){
    			//卖单
    			$list[$k]['username'] = db('user')->where(['id'=>$v['user_id']])->find()['username'];
    		}
    	}
    	$data['list'] = $list;
    	$data['count'] = $count;
    	$this->success('操作成功',$data);
    }
    /**
     * 撤销订单
     */
    public function delOrder(){
    	//订单号
    	$order_num = input('order_num');
    	if(empty($order_num)){
    		$this->error('订单号不能空');
    	}
    	$order = db('put_order')->where(['put_num'=>$order_num])->find();
    	//判断是否是自己的订单
    	if($order['user_id']!=$this->user['id']){
    		$this->error('订单信息错误');
    	}
    	//判断订单是否可以撤销
    	if($order['status']!=1){
    		$this->error('该订单不可撤销');
    	}
    	$data['status'] = 3;
    	$r = db('put_order')->where(['put_num'=>$order_num])->update($data);
        //卖单返回冻结的币
        if($order['type']==2){
            $num = $order['gold_num']-$order['out_num'];
            db('user')->where(['id'=>$this->user['id']])->setDec('gold_freeze',$num);
        }
    	if($r){
    		$this->success('撤销成功');
    	}else{
    		$this->error('撤销失败');
    	}

    }
    /**
     * 订单详情
     */
    public function orderDetail(){
    	//订单号
    	$order_num = input('order_num');
    	if(empty($order_num)){
    		$this->error('订单号不能空');
    	}
    	$order = db('Order')->where(['order_num'=>$order_num])->find();
    	//订单基本信息
    	if($order['type']==1){
    		$usermsg = db('user')->where(['id'=>$order['buy_id']])->find();
    	}
    	if($order['type']==2){
    		$usermsg = db('user')->where(['id'=>$order['sell_id']])->find();
    	}
    	$ordermsg['type'] = $order['type'];
    	$ordermsg['username'] = $usermsg['username'];
    	$ordermsg['addtime'] = date('Y-m-d H:i:s',$order['addtime']);
        $ordermsg['matchtime'] = $order['matchtime']+2*60*60;
    	$ordermsg['price'] = $order['price'];
        $ordermsg['gold_num'] = $order['gold_num'];
        $ordermsg['status'] = $order['status'];
    	$ordermsg['order_num'] = $order['order_num'];
    	// 1有撤销 2无撤销
    	$isdel = 2;
    	//电话信息
    	$phone = [];
    	//银行卡信息
    	$bank = [];
    	//微信信息
    	$wx = [];
    	//支付宝信息
    	$zfb = [];
    	if($order['status']==1){
    		//判断是否有撤销按钮
    		if($order['sell_id']==$this->user['id'] || $order['buy_id']==$this->user['id']){
    			$isdel = 1;
                //自己的未匹配的订单 显示未匹配
                $ordermsg['button'] = 1;
    		}else{
                //不是自己的未匹配的订单  显示开始交易按钮
                $ordermsg['button'] = 2;
            }
    		$phone['phone'] = $usermsg['phone'];
    	}else{
            if($order['status']==2){
                //已经匹配的订单  显示确认打款按钮 并加倒计时
                $ordermsg['button'] = 3;
            }
            if($order['status']==3 && $order['sell_id']==$this->user['id']){
                //已经付款的订单  卖家显示确认付币按钮
                $ordermsg['button'] = 4;
            }
            if($order['status']==3 && $order['buy_id']==$this->user['id']){
                //已经付款的订单  买家显示确认已付款
                $ordermsg['button'] = 6;
            }
            if($order['status']==4 || $order['status']==5){
                //已经完成的订单  不显示按钮
                $ordermsg['button'] = 5;
            }
    		//显示对方的信息
    		if($order['sell_id']==$this->user['id']){
	    		$usermsg = db('user')->where(['id'=>$order['buy_id']])->find();
	    	}
	    	if($order['buy_id']==$this->user['id']){
	    		$usermsg = db('user')->where(['id'=>$order['sell_id']])->find();
	    	}
	    	//电话信息
	    	$phone['phone'] = $usermsg['phone'];
	    	//银行卡信息
	    	$bank['card_name'] = $usermsg['card_name'];
	    	$bank['card_num'] = $usermsg['card_num'];
	    	$bank['card_bank'] = $usermsg['card_bank'];
	    	//微信信息
	    	$wx['wx_num'] = $usermsg['wx_num'];
	    	$wx['wx_payimage'] = $usermsg['wx_payimage'];
	    	//支付宝信息
	    	$zfb['zfb_num'] = $usermsg['zfb_num'];
	    	$zfb['zfb_payimage'] = $usermsg['zfb_payimage'];
    	}
    	$data['ordermsg'] = $ordermsg;
    	$data['isdel'] = $isdel;
    	$data['phone'] = $phone;
    	$data['bank'] = $bank;
    	$data['wx'] = $wx;
    	$data['zfb'] = $zfb;
    	$this->success('操作成功',$data);
    }
    
}
